<?php

class HTreeWidget extends CWidget{
    
    public function run() {
        parent::run();
        $output = "";
        $category = Category::model()->findByPk(1);
        $this->loopNode($output, $category,null,"&nbsp;&nbsp;");
        $this->render('HTree',array('output' => $output));
    }
    
    /**
     * show element
     * @return type 
     */
    private function loopNode(&$output,$category,$parent,$prefix,$spear = "<sup>|___</sup>"){
        if($category != null){
            $newPrefix = "";
            if($category->id != 1 && $parent != null){
                ob_start();
                $this->render('ElementHTree',array('parent' => $parent,'category' => $category,'prefix' => $prefix,'spear' => $spear));
                $out1 = ob_get_contents();
                ob_end_clean();
                $output .= $out1;
                $newPrefix = str_replace('<sup>|___</sup>', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $prefix).$spear;
            }
            $children = $category->children()->findAll(array('order'=>'orders ASC'));
            foreach($children as $child){
                $this->loopNode($output, $child,$category,$newPrefix,$spear);
            }
        }
    }
}
